package com.example.springbootproxy.cglib;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author: wukangrong
 * @date: 2020/7/10 15:53
 * @description:
 */
@SuppressWarnings("unchecked")
@Data
@Slf4j
@AllArgsConstructor
public class ProxyFactory<T> implements MethodInterceptor {

    private T t;

    public <D> D getProxy(Class<D> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return (D) enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        log.info("前置增强...");
       // methodProxy.invokeSuper(t, objects);
        return method.invoke(t, objects);
    }
}
